package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.BaskDetailsActivity;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.custom.MyGridView;
import com.wangtao.firstapp.modles.ShopDetailsData;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 单品详情页面-->用户晒单 的适配器
 * Created by lanouhn on 16/9/10.
 */
public class ShopDetailsAdaopter extends RecyclerView.Adapter<ShopDetailsAdaopter.ShopDetailsViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private ShopDPAdapter shopDPAdapter;

    private Context context;
    private ShopDetailsData shopDetailsData;

    public ShopDetailsAdaopter(Context context, ShopDetailsData shopDetailsData) {
        this.context = context;
        this.shopDetailsData = shopDetailsData;

    }

    @Override
    public ShopDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shopdetails_bask, null);
        ShopDetailsViewHolder holder = new ShopDetailsViewHolder(view);
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();
        return holder;
    }

    @Override
    public void onBindViewHolder(ShopDetailsViewHolder holder, final int position) {

        final List<ShopDetailsData.DataBean.PostListBean> postListBean = shopDetailsData.getData().getPost_list();
        if (!shopDetailsData.getData().getPost_count().equals("")) {
                Picasso.with(context).load(postListBean.get(0).getUser().getAvatar()).into(holder.logo);
                holder.name.setText(postListBean.get(0).getUser().getNickname());
                holder.time.setText(postListBean.get(0).getDatestr());
                holder.content.setText(postListBean.get(0).getContent());
                //图片
//                shopDPAdapter = new ShopDPAdapter(context, postListBean.get(i).getPics());
//                holder.mygridview.setNumColumns(postListBean.get(i).getPics().size());
//                holder.mygridview.setAdapter(shopDPAdapter);
                Picasso.with(context).load(postListBean.get(0).getPics().get(0).getUrl()).into(holder.shopPhone);


        }

//        //跳转至 全部晒单 页面
//        holder.relView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String details_ID = shopDetailsData.getData().getId();
//                Intent intent = new Intent(context, AllBatheActivity.class);
//                intent.putExtra("details_iD", details_ID);
//                context.startActivity(intent);
//            }
//        });
        //跳转至 晒单详情 页面
        holder.rltView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String baskdetails_ID=shopDetailsData.getData().getPost_list().get(position).getId();
                Intent intent=new Intent(context, BaskDetailsActivity.class);
                intent.putExtra("baskdetails_iD",baskdetails_ID);
                context.startActivity(intent);
            }
        });

        //跳转至，作者介绍页面
        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID =  postListBean.get(position).getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

        //跳转至，作者介绍页面
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String author_ID =  postListBean.get(position).getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return shopDetailsData.getData().getPost_list().size();
    }

    public class ShopDetailsViewHolder extends RecyclerView.ViewHolder {

        private ImageView logo;
        private TextView  name, time, content;
        private MyGridView mygridview;

        private RelativeLayout rltView;
        private ImageView shopPhone;

        public ShopDetailsViewHolder(View itemView) {
            super(itemView);

           rltView = (RelativeLayout) itemView.findViewById(R.id.rltView_item_shopdetails_bask);//下边的数据部分的容器，用于隐藏，显示
            logo = (CircleImageView) itemView.findViewById(R.id.logo_item_shopdetails_bask);//作者头像
           // bask = (TextView) itemView.findViewById(R.id.bask_item_shopdetails_bask);//用户晒单 （？）
            name = (TextView) itemView.findViewById(R.id.name_item_shopdetails_bask);//作者昵称
            time = (TextView) itemView.findViewById(R.id.time_item_shopdetails_bask);//晒单时间
            content = (TextView) itemView.findViewById(R.id.content_item_shopdetails_bask);//晒单内容，，文字
//            mygridview = (MyGridView) itemView.findViewById(R.id.mygridview_item_shopdetails_bask);//用于加载晒单的图片
            shopPhone= (ImageView) itemView.findViewById(R.id.shopPhoto_item_shopdetails_bask);//图片
        }
    }

}
